package org.monkey.common.generator.entity;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.monkey.common.constant.entity.MysqlColumnInfo;
import org.monkey.common.constant.util.MonkeyStrUtil;

@Data
@ApiModel
@NoArgsConstructor
public class JavaEntityInfo {
  private String entityName;
  private String entityDescription;
  private String tableName;
  private List<JavaFieldInfo> javaFieldInfos;
  public JavaEntityInfo(String tableName, boolean hump, String tablePre, String entityDescription, List<MysqlColumnInfo> columnInfos) {
    this.tableName = tableName;
    String en = MonkeyStrUtil.upperFirstCase(tableName.toLowerCase());
    if(StrUtil.isNotBlank(tablePre)&&en.toLowerCase().startsWith(tablePre.toLowerCase())){
      en = en.substring(tablePre.length());
    }
    if(hump){
      en = MonkeyStrUtil.underlineToHump(en);
    }
    this.entityName = en;
    this.entityDescription = entityDescription;
    if(columnInfos==null){
      javaFieldInfos = new ArrayList<>();
    }else{
      javaFieldInfos = columnInfos.stream().map(c->new JavaFieldInfo(c,hump)).collect(Collectors.toList());
    }
  }

  public Map<String,Object> toMap(){
    Map<String,Object> map = new HashMap<>();
    map.put("entityName", entityName);
    map.put("entityDescription", entityDescription);
    map.put("tableName", tableName);
    map.put("javaFieldInfos", javaFieldInfos);
    return map;
  }
}
